package com.forum.Service.Impl;

import com.forum.Entity.Comment;
import com.forum.Mapper.CommentMapper;
import com.forum.Service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Override
    public List<Comment> getComments(String postId) {
        List<Comment> comments = commentMapper.getComments(postId);
        for (Comment comment : comments){
            String commentId = comment.getCommentId();
            List<Comment> reply = commentMapper.getReplyComments(commentId);
            comment.setReplies(reply);
        }
        return comments;
    }

    @Override
    public void addComment(Comment comment) {
        comment.setCommentId(makeId());
//        String PartentId =';
//        comment.setParentId(PartentId);
        comment.setLikes(0);
        commentMapper.addComment(comment);
    }

    @Override
    public void addReply(Comment comment) {
        comment.setCommentId(makeId());
        comment.setLikes(0);
        commentMapper.addComment(comment);
    }
    //更新点赞数 加1
    @Override
    public void addlikes(String targetId) {
        Comment comment = commentMapper.getCommentById(targetId);
        comment.setLikes(comment.getLikes() + 1);
        commentMapper.addlikes(comment);
    }
    //取消点赞数 减1
    @Override
    public void cutlikes(String targetId) {
        Comment comment = commentMapper.getCommentById(targetId);
        comment.setLikes(comment.getLikes() - 1);
        commentMapper.addlikes(comment);
    }

    @Override
    public void deletePostComment(String postId) {
        commentMapper.deletePostComment(postId);

    }

    @Override
    public List<Comment> getCommentsByPostId(String postId) {
        return  commentMapper.getCommentsByPostId(postId);
    }

    @Override
    public PageInfo<Comment> getCommentsByPost(Integer pageNum, Integer pageSize, String postId, String content) {
        PageHelper.startPage(pageNum, pageSize);

        return new PageInfo<>(commentMapper.getCommentsByPost(postId, content));
    }

//    @Override
//    public void deleteComment(String commentId) {
//        //删除主评论 点赞
//        commentMapper.deleteComment(commentId);
//        commentMapper.deleteLikes(commentId);
//
//        //查询子评论 遍历删除子评论及点赞
//        List<Comment> replies = commentMapper.getReplies(commentId);
//        for (Comment reply : replies) {
//            commentMapper.deleteComment(reply.getCommentId());
//            commentMapper.deleteLikes(reply.getCommentId());
//        }
//    }
@Override
public void deleteComment(String commentId) {
    // 获取主评论
    Comment mainComment = commentMapper.getCommentById(commentId);
    if (mainComment == null) {
        return; // 如果主评论不存在，直接返回
    }

    String postId = mainComment.getPostId(); // 获取关联的帖子ID
    int totalCommentsToDelete = 1; // 主评论
//    int totalLikesToDelete = mainComment.getLikes(); // 主评论点赞数

    // 删除主评论及其点赞
    commentMapper.deleteComment(commentId);
    commentMapper.deleteLikes(commentId);

    // 查询并处理子评论
    List<Comment> replies = commentMapper.getReplies(commentId);
    for (Comment reply : replies) {
        totalCommentsToDelete++; // 累加子评论数
//        totalLikesToDelete += reply.getLikes(); // 累加子评论点赞数

        // 删除子评论及其点赞
        commentMapper.deleteComment(reply.getCommentId());
        commentMapper.deleteLikes(reply.getCommentId());
    }

    // 更新帖子的评论数和点赞数
    commentMapper.updatePostCommentCount(postId, totalCommentsToDelete);
//    commentMapper.updatePostLikeCount(postId, totalLikesToDelete);
}

    private String makeId() {
        return "CM" + System.currentTimeMillis();
    }
//    private String makeParentId() {
//        return "FU"+ System.currentTimeMillis();
//    }
}
